#define _CRT_SECURE_NO_WARNINGS 1

#include <string>
#include <vector>
#include <unordered_map>
using namespace std;

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string echo = "croak";
        int n = echo.size();
        vector<int> hash(n);
        unordered_map<char, int> index;
        for (int i = 0; i < n; ++i)
        {
            index[echo[i]] = i;
        }
        for (auto ch : croakOfFrogs)
        {
            if (ch == echo[0])
            {
                if (hash[n - 1] != 0) --hash[n - 1];
                ++hash[0];
            }
            else
            {
                int pos = index[ch];
                if (hash[pos - 1] == 0) return -1;
                --hash[pos - 1];
                ++hash[pos];
            }
        }
        for (int i = 0; i < n - 1; ++i)
            if (hash[i] != 0) return -1;
        return hash[n - 1];
    }
};